;; Ron Gosa
;; Language Lab 2
;; October 11, 2008
;; CSC430, Mike Quals

;; The membership function compares given atoms and elements of list one at a time until either a
;; match is found or there are no more elements left in the list.

(define (membership atm lis)
  (cond
    ((null? lis) #F)
    ((eq? atm (car lis)) #T)
    (else (membership atm (cdr lis)))
    ))


;; The equalsimp function determines if two given lists are equal.

(define (equalsimp lis1 lis2)
  (cond
    ((null? lis1) (null? lis2))
    ((null? lis2) #F)
    ((eq? (car lis1) (car lis2))
    (equalsimp (cdr lis1) (cdr lis2)))
    (else #F)
    ))


;; The equal function compares two lists (or expressions) and determines if they are equal.

(define (equal lis1 lis2)
  (cond
    ((not (list? lis1)) (eq? lis1 lis2))
    ((not (list? lis2)) #F)
    ((null? lis1) (null? lis2))
    ((null? lis2) #F)
    ((equal (car lis1) (car lis2))
     (equal (cdr lis1) (cdr lis2)))
    (else #F)
    ))


;; The append_me function constructs a new list containing all elements of two given lists.

(define (append_me lis1 lis2)
  (cond
    ((null? lis1) lis2)
    (else (cons (car lis1) (append_me (cdr lis1) lis2)))
    ))


;; The guess function yields a simple list containing common elements of two given lists. For
;; example, if sets are provided this function provides a list representing the intersection of
;; both lists. The guess function uses the member function defined above.

(define (guess lis1 lis2)
  (cond
    ((null? lis1) '())
    ((member (car lis1) lis2)
     (cons (car lis1) (guess (cdr lis1) lis2)))
    (else (guess (cdr lis1) list))
   ))


;; The adder function determines the sum of a list of numeric atoms by repeatedly adding the car of
;; the lisr to the sum of its cdr.

(define (adder lis)
  (cond
    ((null? lis) 0)
    (else (eval (cons '+ lis)))
    ))


;; Exercise 6, pg. 682
;; Write a Scheme function that removes the last element from a given list.
;; This function uses recursion to delete the last element of the list. If more than one element
;; exists the function calls itself omitting the first element of the list. When the given list
;; contains a single element then that element is removed.

(define (remove_last lis)
  (cond
    ((null? lis) (print "Cannot remove element: list is empty!"))   ; verify list is populated
    ((null? (cdr lis)) (remove (car lis) lis))   ; remove if only one element
    ((cons (car lis) (remove_last (cdr lis))))   ; recursive call omitting first element
    )) 


;; Unit test for the remove_last function

(define my_list (list 1 2 3 4))                                         ; build 4 element list
(display "Original list: ") (display my_list) (newline)                 ; output results 
(display "Modified list: ") (display (remove_last my_list)) (newline)   ; remove last list element

